package il.technion.cs236369.webserver;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * This class handles sessions 
 * @author Yuval and Eddie
 *
 */
public class SessionsHandler {
	
	private ConcurrentHashMap<UUID,Session> sessions;	//holds all active sessions in server

	public SessionsHandler() {
		//create sessions map
		this.sessions = new ConcurrentHashMap<UUID,Session>();
	}
	
	
	/**
	 * starts a new active session
	 * @return the new session
	 */
	public Session startNewSession(){
		Session session = new Session(this);
		sessions.put(session.getSessionID(), session);
		return session;
	}
	
	public void addSession(Session session){
		sessions.put(session.getSessionID(), session);
	}
	
	/**
	 * Removes an active session- that means the session will be deleted soon by the garbage collector
	 * Does nothing if there is not such a session
	 * @param uniqueSessionID - the session ID
	 */
	public void removeSession(UUID uniqueSessionID){
		sessions.remove(uniqueSessionID);
	}
	
	public boolean isSessionActive(UUID uniqueSessionID){
		return sessions.containsKey(uniqueSessionID);
	}
	
	public Session getSession(UUID uniqueSessionID){
		return sessions.get(uniqueSessionID);
	}

}
